import serial
# from numpy import *
import time
import sqlite3
import string
import re
import sys

f = open('./db.sqlite', 'w')
f.close()
conn = sqlite3.connect('./db.sqlite')

c = conn.cursor()
c.execute('''CREATE TABLE "data" ("source" INTEGER NOT NULL ,"time" DOUBLE NOT NULL ,"num" INTEGER NOT NULL ,"temp" INTEGER NOT NULL );''')
conn.commit()


print "Setting up port"
port=serial.Serial(sys.argv[1],9600)

PATTERN = ".*\*{2}(\d*):(\d*):(\d*).*"
COMPILED_PATTERN = re.compile(PATTERN,re.DOTALL)

while 1:
    
    line=port.readline()
    print line
    results = COMPILED_PATTERN.match(line)
    # Can get the group in results.group(1)
    if results is not None:
        print results.groups()
        c.execute('insert into data values (?,?,?,?)', (results.group(1),round(time.time(),4),results.group(2),results.group(3)))
        conn.commit()
